package com.epalmpay.controller.apiweixin;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.epalmpay.commom.BaseController;
import com.epalmpay.service.member.IMemberService;
import com.epalmpay.service.weixin.IWxBaseService;
import com.epalmpay.service.weixin.impl.WxService;
import com.epalmpay.util.PageData;
import com.epalmpay.util.Tools;
import com.epalmpay.entity.Member;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;

/**
 * 
 * 抽奖 网页授权
 * 
 * @author zsj 创建时间：2018年4月28日
 */
@Controller
@RequestMapping(value = "/api/lottery")
public class WechatLotteryOAuth2Controller extends BaseController {

	@Autowired
	protected WxService wxService;

	@Autowired
	protected IWxBaseService wxBaseService;
	@Resource
	private IMemberService memberService;

    
	    


	@RequestMapping(value = "index", method = { RequestMethod.GET, RequestMethod.POST })
	public void wechatOpenId(HttpServletRequest request, HttpServletResponse response,String groupId,String path) throws Exception {
		logBefore(logger, "------------------------------------获取抽奖 网页授权微信openId第1步开始------------------------------------"+path);
		wxService.setWxMpConfigStorage(wxBaseService.getTokenByGroupId(Long.valueOf(groupId)));
		String state = "";
		String url = HOME_URL + "/api/lottery/wxCallbackCode";
		if(path.startsWith("http")){
			state = java.net.URLEncoder.encode(path, "utf-8");
		}else{
			state = java.net.URLEncoder.encode("http://"+path, "utf-8");
		}
		wxService.setWxMpConfigStorage(wxBaseService.getTokenByGroupId(Long.valueOf(groupId)));
		String href = wxService.oauth2buildAuthorizationUrl(url+"?groupId="+groupId, WxConsts.OAUTH2_SCOPE_BASE, state);
		logBefore(logger, "------------------------------------获取抽奖 网页授权微信openId第1步结束-----------------------------------");
		response.sendRedirect(href);
	}

	@RequestMapping(value = "wxCallbackCode", method = { RequestMethod.GET, RequestMethod.POST })
	public String wxCallbackCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logBefore(logger, "------------------------------------获取抽奖 网页授权微信openId第2步开始------------------------------------");
		PageData pd = new PageData();
		pd = this.getPageData();
		String groupId = pd.getString("groupId");
		String code = pd.getString("code");
		String state = pd.getString("state");
		wxService.setWxMpConfigStorage(wxBaseService.getTokenByGroupId(Long.parseLong(groupId)));
		WxMpOAuth2AccessToken wxMpOAuth2AccessToken = this.wxService.oauth2getAccessToken(code);
		String openId = wxMpOAuth2AccessToken.getOpenId();
		
		if (Tools.isEmpty(openId)) {
			response.sendRedirect(state);
		} else {
			Member member = memberService.findByOpenId(openId, Long.valueOf(groupId));
			StringBuffer bf=new StringBuffer();
			String begin=state.contains("?")?state:state+"?";
			bf.append(begin).append("memberId=").append(member.getId()).append("&walletStatus=").append(member.getWalletStatus());
			response.sendRedirect(bf.toString());
		}
		logBefore(logger, "------------------------------------获取抽奖 网页授权微信openId第2步结束------------------------------------");
		return null;
	}

	
	
	
	

}
